const fs = require('fs')

const useRoutes = function (app) {
  let filePath = ''
  function _getRoutes(path = '') {
    fs.readdirSync(__dirname + path).forEach((file) => {
      if (file === 'index.js') return
      if (!/\.js$/.test(file)) {
        filePath += `/${file}`
        _getRoutes(filePath)
      } else {
        const router = require(`.${path}/${file}`)
        app.use(router.routes())
        app.use(router.allowedMethods())
        filePath = ''
      }
    })
  }
  _getRoutes(filePath)
}

module.exports = useRoutes
